package com.mercenary.wein.screenControl.service;

import com.mercenary.wein.screenControl.bean.CommandBean;
import com.mercenary.wein.screenControl.bean.CommandDetailBean;
import com.mercenary.wein.screenControl.bean.ScheduleTask;
import com.mercenary.wein.screenControl.command.jobSchedual.QuartzManager;
import com.mercenary.wein.screenControl.dao.CommandDao;
import com.mercenary.wein.screenControl.dao.ConnectDb;
import com.mercenary.wein.screenControl.ui.util.Constants;
import org.apache.commons.dbutils.DbUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;
import org.quartz.SchedulerException;

import java.sql.Connection;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;

/**
 * Created by tuxiao on 2017/3/25.
 */
public class CommandService {
    private static CommandDao commandDao = new CommandDao();

    public List<CommandDetailBean> getCommandList(){
        return commandDao.getCommandList();
    }

    /**
     * 查看设备是否有正在运行的任务
     * @param did
     * @return long[id,submitime]
     */
    public long[] isRunning(String did){
        return commandDao.isRunning(did);
    }

    public void shutdownTask(long id){
        commandDao.shutdownTask(id);
    }

    public void updateTask(long id,int status){
        commandDao.updateTask(id,status);
    }

    public long insertTask(CommandBean commandBean){
        return commandDao.insertTask(commandBean);
    }

    public List<ScheduleTask> getScheduleTask(){
        return commandDao.getScheduleTask();
    }

    public void deleteSchedualTask(long id){
        commandDao.deleteSchedualTask(id);
    }

    public List<ScheduleTask> getSchdualJobs(){
        return commandDao.getSchdualJobs();
    }

    public void removeJob(ScheduleTask task){
        List<ScheduleTask> tasks = commandDao.getScheduleTaskByZid(task.getZid());
        for(ScheduleTask t:tasks){
            try {
                QuartzManager.removeJob(t.getCommandBean());
            } catch (SchedulerException e) {
                e.printStackTrace();
            }
        }
        commandDao.removeJob(task.getZid());
     }
}
